<?php
namespace App\Controllers;

class Badminton extends BaseController
{

    public function index()
    {
        $players = new \App\Models\Players();
        // retrieve all the records
        $records = $players->findAll();

        $table = new \CodeIgniter\View\Table();

        $headings = $players->db->getFieldNames('players');
        $displayHeadings = array_slice($headings, 1, 2);
        $table->setHeading(array_map('ucfirst', $displayHeadings));
       
        foreach ($records as $record) {
            $nameLink = anchor("badminton/showme/" . $record['id'], $record['name']);
            $table->addRow($nameLink, $record['description']);
            }


        foreach ($records as $record) {
            $nameLink = anchor("badminton/showme/$record->id", $record->name);
            $table->addRow($nameLink, $record->description);
        }
        $template = [
            'table_open' => '<table cellpadding="5px">',
            'cell_start' => '<td style="border: 1px solid #dddddd;">',
            'row_alt_start' => '<tr style="background-color:#dddddd">',
        ];
        $table->setTemplate($template);
        $fields = [
            'title' => 'Badminton Players',
            'heading' => 'Badminton Players',
            'footer' => 'Copyright LHX'
        ];
        $parser = \Config\Services::parser();

        return $parser->setData($fields)
            ->render('templates\top') .
            $table->generate() .
            $parser->setData($fields)
                ->render('templates\bottom');
    }


    public function showme($id)
    {
        $players = new \App\Models\Players();
        // retrieve all the records
        $record = $players->find($id);
        $parser = \Config\Services::parser();
        // tell it about the substitions
        return $parser->setData($record)
            // and have it render the template with those
            ->render('oneplayer');
    }




    public function edit($id = NULL)
    {
        $players = new \App\Models\Players();
        $record = $players->find($id);
        $fields = [
            'title' => 'Edit a Badminton Player',
            'heading' => 'Edit a Badminton Player',
            'footer' => 'Copyright LHX'
        ];
        $parser = \Config\Services::parser();
        return $parser->setData($fields)
            ->render('templates\top') .
            $parser->setData($record)
                ->render('editplace') .
            $parser->setData($fields)
                ->render('templates\bottom');
    }
    public function create($id = NULL)
    {
        $fields = [
            'title' => 'Create a Badminton Player',
            'heading' => 'Create a Badminton Player',
            'footer' => 'Copyright LHX'
        ];
        $record = [
            'id' => 0,
            'name' => '',
            'status' => '',
            'description' => '',
            'features' => '',
            'hobbies' => '',
            'ability' => '',
            'link' => '',
            'image' => '',
        ];

        $parser = \Config\Services::parser();
        return $parser->setData($fields)
            ->render('templates\top') .
            $parser->setData($record)
                ->render('editplace') .
            $parser->setData($fields)
                ->render('templates\bottom');
    }


    public function handle()
    {
        $players = new \App\Models\Players();
        helper(['form', 'url']);
        $id = $this->request->getPost('id');
        $record = $players->find($id);
        if ($this->validate($players->validationRules)) {
            if ($id == 0) {
            $players->save([
                'id' => $this->request->getPost('id'),
                'name' => $this->request->getPost('name'),
                'status' => $this->request->getPost('status'),
                'description' => $this->request->getPost('description'),
                'features' => $this->request->getPost('features'),
                'hobbies' => $this->request->getPost('hobbies'),
                'ability' => $this->request->getPost('ability'),
                'link' => $this->request->getPost('link'),
                'image' => $this->request->getPost('image'),
            ]);
        }else{
            $players->save([
                'id' => $this->request->getPost('id'),           
                'name' => $this->request->getPost('name'),      
                'status' => $this->request->getPost('status'),   
                'description' => $this->request->getPost('description'), 
                'features' => $this->request->getPost('features'), 
                'hobbies' => $this->request->getPost('hobbies'), 
                'ability' => $this->request->getPost('ability'), 
                'link' => $this->request->getPost('link'),       
                'image' => $this->request->getPost('image')     
            ]);
            
        }
        $fields = [
            'title' => 'Edit a Badminton Player',
            'heading' => 'Edit a Badminton Player',
            'footer' => 'Copyright LHX'
        ];
        $parser = \Config\Services::parser();
        //Top of page
        $returnView = $parser->setData($fields)->render('templates\top');

        if (!$this->validate($players->validationRules)) {
            $returnView .= $parser->renderString($this->validator->listErrors());
            $returnView .= $parser->setData($this->request->getPost())
                ->render('editplayer');
        } else {
            $returnView .= $parser->renderString("<p>Success!</p>");
            $returnView .= $parser->setData($this->request->getPost())
                ->render('editplayer');
        }

        $returnView .= $parser->setData($fields)
            ->render('templates\bottom');

        return $returnView;

    }


    }
}